<template>
    <el-dialog
        :center="true"
        :show-close="false"
        :visible="show"
        @close="close"
        lock-scroll
        title="资讯发布"
    >
        <el-form label-width="40px">
            <el-form-item class="marginBottom" label="标题">
                <el-input class="borderNone" clearable placeholder="请输入标题" v-model="sub.title"></el-input>
            </el-form-item>
            <el-form-item class="marginBottom" label="内容">
                <el-input
                        class="borderNone"
                        type="textarea"
                        :autosize="{ minRows: 3}"
                        placeholder="请输入内容"
                        clearable
                        v-model="sub.content"
                ></el-input>
            </el-form-item>
            <el-form-item class="marginBottom" label="类型">
                <el-select v-model="sub.msgType" placeholder="请选择">
                    <el-option v-for="item in msgData" :key="item.id" :label="item.label" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="marginBottom">
                <el-button @click="submit" type="primary">发布</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
</template>

<script>
    export default {
        name: "index",
        props : {
          show : {
              type : Boolean,
              default : false,
          },
          userId : {
              type : Number,
          }
        },
        data(){
            return {
                sub : {
                    title: "",
                    content: "",
                    msgType: 2,
                    userId : this.userId,
                    createDate : new Date().toLocaleDateString(),
                    flag : 2, // 默认待审核
                },
                msgData : [
                    {id : 1 , label : "公告"},
                    {id : 2 , label : "校园资讯"},
                    {id : 3 , label : "寻物启事"},
                    {id : 4 , label : "其他"},
                ]
            }
        },
        methods : {
            submit(){
                this.$api.message.messageAdd(this.sub)
                    .then(() => {
                        this.$message({
                            message : "发布成功,请等待审核通过!",
                            type : "success",
                        });
                        this.close();
                    })
                .catch(error => {
                    this.message.error(error.message);
                })
            },
            close(){
                this.$emit("close");
            }
        }
    }
</script>

<style scoped>

</style>
